<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>canvas</title>
  <style>
    canvas {
      box-shadow: 8px 8px 20px #CCC;
      margin-left: 20px;
      margin-top: 20px;
    }
  </style>
</head>

<body>
  <canvas id="canvas" width="500" height="500">
    你的浏览器太low了~
  </canvas>
  <script>

    window.onload = function () {
      var canvas = document.getElementById('canvas')
      console.info();
      if (canvas.getContext) {
        var ctx = canvas.getContext('2d');
        // 添加鼠标按下事件
        canvas.onmousedown = function (e) {
          var ev = e || window.event; // event兼容
          // 获取鼠标按下位置
          var x = ev.clientX - canvas.offsetLeft; // 可视区域x偏移量
          var y = ev.clientY - canvas.offsetTop;

          ctx.lineWidth = 3;
          ctx.strokeStyle = 'green';


          ctx.beginPath();
          ctx.moveTo(x, y);


          canvas.onmousemove = function (e) {
            var ev = e || window.event;
            var x = ev.clientX - canvas.offsetLeft;
            var y = ev.clientY - canvas.offsetTop;
            ctx.lineTo(x, y);
            ctx.stroke()
          }

          canvas.onmouseup = function () {
            canvas.onmousemove = null
          }

        }
      } else {
        alert('你的浏览器太low了~')
      }
    }
  </script>

</body>

</html>